﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

using Waiter.ServiceReference1;
using System.Diagnostics;
// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace Waiter.UserControls
{
    public sealed partial class GoiMonControl : UserControl
    {
        Table table = null;

        // Cái này dùng cho gọi món, 
        // đặt làm biến "toàn cục" để chia sẻ giữa các method cho dễ.
        Item item = null;

        public GoiMonControl()
        {
            this.InitializeComponent();
            init();
        }

        public GoiMonControl(Table tb)
        {
            this.InitializeComponent();
            this.table = tb;
            init();
        }
        private async void init()
        {
            Service1Client client = new Service1Client();
            List<Category> ctg = await client.GetAllCategoriesAsync();
            Debug.WriteLine("GoiMonUserControl init: got " + ctg.Count+ " categories");
            this.DanhMucListView.ItemsSource = ctg;
            this.ItemListView.Height = Window.Current.Bounds.Height;
            this.ItemListView.Width = Window.Current.Bounds.Width;
        }

        

        private async void DanhMucListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Category c = (Category) (sender as ListView).SelectedItem;
            Service1Client client = new Service1Client();
            List<Item> itemsList = await client.GetItemsByCategoryAsync(c);
            ItemListView.ItemsSource = itemsList;
        }

        private void ItemListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if ((sender as ListView).SelectedIndex != -1)
            {
                item = (Item)(sender as ListView).SelectedItem;
                SoLuongDialog slDialog = new SoLuongDialog(this.table.TableName, item.Name);
                slDialog.CloseRequested += slDialog_CloseRequested;
                slDialog.OkRequested += slDialog_OkRequested;
                SoLuongPopup.Child = slDialog;
                SoLuongPopup.IsOpen = true;
            }
           
        }

        private async void slDialog_OkRequested(object sender, EventArgs e)
        {
            int soLuongGoi = (e as NumberEventArgs).number;
            Service1Client client = new Service1Client();
            await client.GoiMonAsync(this.table, item, soLuongGoi);
            this.SoLuongPopup.IsOpen = false;
        }

        private void slDialog_CloseRequested(object sender, EventArgs e)
        {
            this.SoLuongPopup.IsOpen = false;
        }
    }
}
